package com.yh.controller.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yh.anno.LoginInterceptor;
import com.yh.service.LoginService;
import com.yh.utils.Constants;
import com.yh.vo.LoginVO;
import com.yh.vo.ResponseVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yuhuan
 * @date 2022/01/13
 */
@RestController
@RequestMapping("/api/login")
public class LoginWeb {
    /**
     * 日志
     */
    private static final Logger logger = Logger.getLogger(LoginWeb.class);

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    @DubboReference(check = false)
    private LoginService loginService;

    /**
     * 用户登录
     * 服务地址 http://localhost:8080/api/login/user/server
     */
    @RequestMapping("/user/server")
    public ResponseVO loginToUser() throws JsonProcessingException {
        logger.debug(">>>>> 用户登录请求");
        // 获取登录信息
        LoginVO loginVO = getLoginDataFromRedis();
        // 开始登录
        LoginVO vo = loginService.userLogin(loginVO.getUserAccount(), loginVO.getPassWord());
        if (vo.getLoginStatus() == Constants.ResponseDataState.RESP_SUCCESS) {
            return ResponseVO.buildSuccessInstance(vo);
        }
        return ResponseVO.buildSuccessInstance(vo.getLoginStatus());
    }

    /**
     * 开发者登录
     * 服务地址 http://localhost:8080/api/login/developer/server
     */
    @RequestMapping("/developer/server")
    public ResponseVO loginToDeveloper() throws JsonProcessingException {
        logger.debug(">>>>> 开发者登录请求");
        // 获取登录信息
        LoginVO loginVO = getLoginDataFromRedis();
        // 登录
        LoginVO vo = loginService.developerLogin(loginVO.getUserAccount(), loginVO.getPassWord());
        return vo.getLoginStatus() == Constants.ResponseDataState.RESP_SUCCESS ?
                ResponseVO.buildSuccessInstance(vo) : ResponseVO.buildSuccessInstance(vo.getLoginStatus());
    }

    /**
     * 登录请求入口，处理非法的用户登录请求 如：未携带登录信息或者身份不是普通用户或者开发者
     * 服务地址 http://localhost:8080/api/login/server
     */
    @RequestMapping("/server")
    @LoginInterceptor
    public ResponseVO login() {
        logger.debug(">>>>> 非法登录");
        return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.ILLEGAL_LOGIN);
    }

    /**
     * 处理过期身份的登录请求
     * 服务地址 http://localhost:8080/api/login/expired/server
     */
    @RequestMapping("/expired/server")
    public ResponseVO loginFromExpired() {
        logger.debug(">>>>> 登录身份过期");
        return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.LOGIN_EXPIRED);
    }

    /**
     * 退出登录
     * 服务地址 http://localhost:8080/api/login/sign/out
     */
    @RequestMapping("/sign/out")
    public ResponseVO signOut(String userId) {
        logger.debug(">>>>> 退出登录");
        loginService.signOut(userId);
        return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.RESP_SUCCESS);
    }

    /**
     * 从redis获取登录信息
     */
    public LoginVO getLoginDataFromRedis() throws JsonProcessingException {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        // 取出登录信息
        String loginData = (String) valueOperations.get(Constants.LoginData.LOGIN_DATA_PREFIX_KEY);
        LoginVO loginVO = objectMapper.readValue(loginData, LoginVO.class);
        // 删除登录信息
        redisTemplate.delete(Constants.LoginData.LOGIN_DATA_PREFIX_KEY);
        return loginVO;
    }
}
